package com.cgy.utils.redis.server;

import com.cgy.utils.netx.config.ServerConfig;
import com.cgy.utils.netx.server.INetworkServerLibrary;
import com.google.common.base.Preconditions;
import redis.embedded.RedisServer;

import java.io.IOException;

/**
 * @author CHTK
 */
public class RedisTestServer implements INetworkServerLibrary<RedisServer> {

    private RedisServer redisServer;

    @Override
    public INetworkServerLibrary<RedisServer> config(ServerConfig serverConfig) {
        Preconditions.checkArgument(null != serverConfig);
        Preconditions.checkArgument(serverConfig.getPort() > 0);
        try {
            redisServer = new RedisServer(serverConfig.getPort());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public void start() throws Exception {
        redisServer.start();
    }

    @Override
    public void stop() throws Exception {
        redisServer.stop();
    }

    @Override
    public RedisServer getServer() throws Exception {
        return redisServer;
    }
}
